﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Leo.Entities;

namespace Leo.Repository
{
    public class DetalleVentaRepository:MasterRepository,IDetalleVentaRepository
    {
        public List<DetalleVenta> GetAllFromDetVenta()
        {
            var query = from p in context.DetalleVentas
                        select p;

            return query.ToList();
        }

        public List<DetalleVenta> GetFromDetVentaByCriterio(string criterio)
        {
            var query = from p in context.DetalleVentas
                        select p;
            if (criterio != "" & criterio != null & criterio != string.Empty)
            {
                query = from p in query
                        where p.Producto.Nombre.ToUpper().Contains(criterio.ToUpper()) 
                        select p;
            }

            return query.ToList();
        }

        public DetalleVenta GetFromDetVentaById(int id)
        {
            return context.DetalleVentas.Find(id);
        }

        public void AddLote(DetalleVenta Detalleventa)
        {
            context.DetalleVentas.Add(Detalleventa);
            context.SaveChanges();
        }

        public void UpdateDetVenta(DetalleVenta Detalleventa)
        {
            context.Entry(Detalleventa).State = System.Data.EntityState.Modified;
            context.SaveChanges();
        }

        public void RemoveDetVenta(int id)
        {
            var existe = context.DetalleVentas
                        .Where(p => p.Id.Equals(id))
                        .SingleOrDefault();

            if (existe != null)
            {
                context.DetalleVentas.Remove(existe);
                context.SaveChanges();
            }
        }

        
    }
}
